/* GUI CONVERTED from rescrap.p (converter v1.76) Sun Aug 25 20:59:54 2002 */
/* rescrap.p - REPETITIVE SCRAP TRANSACTION                                   */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.5.3.5 $                                                               */
/* REVISION: 7.3      LAST MODIFIED: 10/31/94   BY: WUG *GN77*                */
/* REVISION: 7.3      LAST MODIFIED: 03/29/95   BY: dzn *F0PN*                */
/* REVISION: 7.3      LAST MODIFIED: 02/21/96   BY: jym *G1M9*                */
/* REVISION: 7.3      LAST MODIFIED: 03/19/96   BY: jym *G1PZ*                */
/* REVISION: 7.3      LAST MODIFIED: 05/16/96   BY: *G1VW* Julie Milligan     */
/* REVISION: 7.3      LAST MODIFIED: 07/02/96   BY: *G1Z7* Julie Milligan     */
/* REVISION: 8.6      LAST MODIFIED: 06/11/96   BY: *K001* Alma Lara          */
/* REVISION: 8.6      LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 10/25/99   BY: *N002* Bill Gates         */
/* REVISION: 9.1      LAST MODIFIED: 11/17/99   BY: *N04H* Vivek Gogte        */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KP* Mark Brown         */
/* Revision: 1.5.3.4    BY: Robin McCarthy       DATE: 10/01/01  ECO: *P025*  */
/* $Revision: 1.5.3.5 $         BY: Vivek Gogte          DATE: 08/06/02  ECO: *N1QQ*  */

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/*V8:ConvertMode=Maintenance                                                  */

{mfdtitle.i "2+ "}

{gldydef.i new}
{gldynrm.i new}

define buffer next_wr_route for wr_route.
define buffer prev_wr_route for wr_route.
define buffer ptmstr for pt_mstr.
define variable batchqty as decimal.
define variable comp as character.
define variable conv_qty_inque_scrap as decimal.
define variable conv_qty_outque_scrap as decimal.
define variable conv_qty_rejque_scrap as decimal.
define variable cumwo_lot as character.
define variable ophist_recid as recid.
define variable rejected like mfc_logical.
define variable schedwo_lot as character.
define variable trans_type as character.
define variable undo_stat like mfc_logical no-undo.
define variable undostat like mfc_logical no-undo.
define variable yn like mfc_logical.
define variable qty_inque_adj as decimal.
define variable qty_outque_adj as decimal.
define variable qty_rejque_adj as decimal.
define variable prev_op as integer.
define variable first_op like mfc_logical.
define variable last_op like mfc_logical.
define variable i as integer.
define variable j as integer.
define variable msg_ct as integer.

{retrform.i new}
define new shared variable rsn_codes as character extent 10.
define new shared variable quantities like wr_qty_comp extent 10.
define new shared variable inque_scrap_rsn_codes as character
   extent 10.
define new shared variable inque_scrap_quantities like wr_qty_comp
   extent 10.
define new shared variable outque_scrap_rsn_codes as character
   extent 10.
define new shared variable outque_scrap_quantities like wr_qty_comp
   extent 10.
define new shared variable rejque_scrap_rsn_codes as character
   extent 10.
define new shared variable rejque_scrap_quantities like wr_qty_comp
   extent 10.
define new shared variable h_wiplottrace_procs as handle no-undo.
define new shared variable h_wiplottrace_funcs as handle no-undo.
{wlfnc.i} /*FUNCTION FORWARD DECLARATIONS*/
{wlcon.i} /*CONSTANTS DEFINITIONS*/

if is_wiplottrace_enabled() then do:
   {gprunmo.i &program=""wlpl.p"" &module="AWT"
      &persistent="""persistent set h_wiplottrace_procs"""}
   {gprunmo.i &program=""wlfl.p"" &module="AWT"
      &persistent="""persistent set h_wiplottrace_funcs"""}
end.

/* DO NOT RUN PROGRAM UNLESS QAD_WKFL RECORDS HAVE */
/* BEEN CONVERTED SO THAT QAD_KEY2 HAS NEW FORMAT  */
if can-find(first qad_wkfl where qad_key1 = "rpm_mstr") then do:
   /* UTILITY: utrecov2.p MUST BE RUN PRIOR TO EXECUTING THIS PROGRAM */
   {pxmsg.i &MSGNUM=5126 &ERRORLEVEL=3}
   message.
   message.
   leave.
end.

eff_date = today.
find first gl_ctrl no-lock.

{gprun.i ""redflt.p""}
/*GUI*/ if global-beam-me-up then undo, leave.


main:
repeat:
/*GUI*/ if global-beam-me-up then undo, leave.

   /*GET EMP, EFFDATE, SHIFT, SITE, CONTROLTOTAL FROM USER*/
   {gprun.i ""retrin1.p"" "(output undo_stat)"}
/*GUI*/ if global-beam-me-up then undo, leave.

   if undo_stat then undo, leave.

   wo-loop:
   repeat:
/*GUI*/ if global-beam-me-up then undo, leave.

      /*GET ITEM, OP, LINE FROM USER*/
      {gprun.i ""retrin2.p"" "(output undo_stat)"}
/*GUI*/ if global-beam-me-up then undo, leave.

      if undo_stat then undo, leave.

      /*FIND REPETITIVE SCHEDULE TO UPDATE*/
      {gprun.i ""reoptr1b.p""
         "(input site, input line, input part, input op, input eff_date,
           output routing, output bom_code, output schedwo_lot)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      if schedwo_lot = "?" then do:
         /* Unexploded schedule with consumption period */
         {pxmsg.i &MSGNUM=325 &ERRORLEVEL=3}
         next-prompt part.
         undo, retry.
      end.

      /*GET BOM, ROUTING FROM USER*/
      {gprun.i ""retrin3.p"" "(output undo_stat)"}
/*GUI*/ if global-beam-me-up then undo, leave.

      if undo_stat then undo, leave.

      /*FIND CUM ORDER.  */
      {gprun.i ""regetwo.p"" "(input site, input part, input eff_date,
           input line, input routing, input bom_code, output cumwo_lot)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      /*VALIDATE THE OPERATION */
      {reopval.i &part = part
         &routing = routing
         &op = op
         &date = eff_date
         &prompt = op
         &frame = "a"
         &leave = ""no""
         &loop = "main"
         }

      /* CREATE IT IF IT DOESN'T EXIST*/
      if cumwo_lot = ? then do:
         {gprun.i ""recrtwo.p"" "(input site, input part,
              input eff_date,
              input line, input routing, input bom_code, output cumwo_lot)"}
/*GUI*/ if global-beam-me-up then undo, leave.


         if cumwo_lot = ? then next wo-loop.
         display cumwo_lot @ wo_lot with frame a.
      end.
      else do:
         display cumwo_lot @ wo_lot with frame a.
         find wo_mstr where wo_lot = cumwo_lot no-lock.

         if wo_status = "c" then do:
            /* CUM ORDER IS CLOSED */
            {pxmsg.i &MSGNUM=5101 &ERRORLEVEL=3}
            undo, retry.
         end.
      end.

      find wr_route where wr_lot = cumwo_lot and wr_op = op no-lock
         no-error.
      if not available wr_route then do:
         /* OPERATION DOES NOT EXIST */
         {pxmsg.i &MSGNUM=106 &ERRORLEVEL=3}
         next-prompt op with frame a.
         undo, retry.
      end.

      if not wr_milestone then do:
         /* OPERATION NOT A MILESTONE */
         {pxmsg.i &MSGNUM=560 &ERRORLEVEL=2}

         if is_wiplottrace_enabled() then do:
            if prev_milestone_operation(cumwo_lot, op) <> ?
               and is_operation_queue_lot_controlled
               (cumwo_lot,
               prev_milestone_operation(cumwo_lot, op),
               OUTPUT_QUEUE)
            then do:
               {pxmsg.i &MSGNUM=8465 &ERRORLEVEL=3}
               /*WIP LOT TRACE IS ENABLED AND OPERATION IS A NON-MILESTONE */
               undo, retry.
            end.
         end.
      end.

      find first wr_route where wr_lot = cumwo_lot and wr_op > op
      no-lock no-error.
      last_op = not available wr_route.
      find last wr_route where wr_lot = cumwo_lot and wr_op < op
      no-lock no-error.

      if available wr_route then do:
         first_op = no.
         prev_op = wr_op.
      end.
      else do:
         first_op = yes.
         prev_op = ?.
      end.

      find wr_route where wr_lot = cumwo_lot and wr_op = op no-lock
         no-error.

      display wr_desc with frame a.

      assign
         wkctr = ""
         mch = ""
         dept = ""
         um = ""
         conv = 1
         qty_inque = 0
         inque_rsn_code = ""
         inque_multi_entry = no
         qty_outque = 0
         outque_rsn_code = ""
         outque_multi_entry = no
         qty_rejque = 0
         rejque_rsn_code = ""
         rejque_multi_entry = no
         rsn_codes = ""
         quantities = 0
         inque_scrap_rsn_codes = ""
         inque_scrap_quantities = 0
         outque_scrap_rsn_codes = ""
         outque_scrap_quantities = 0
         rejque_scrap_rsn_codes = ""
         rejque_scrap_quantities = 0
         .

      {gprun.i ""resetmno.p""
         "(input cumwo_lot, input op, output move_next_op)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      wkctr = wr_wkctr.
      mch = wr_mch.
      find wc_mstr where wc_wkctr = wkctr and wc_mch = mch no-lock.

      dept = wc_dept.
      find dpt_mstr where dpt_dept = wc_dept no-lock no-error.

      find wo_mstr where wo_lot = cumwo_lot no-lock.
      find pt_mstr where pt_part = wo_part no-lock.
      um = pt_um.

      find first ea_mstr no-lock where ea_type = "1" no-error.
      if available ea_mstr then earn_code = ea_earn.

      display
         wkctr
         mch
         wc_desc
         dept
         dpt_desc                when (available dpt_mstr)
         ""                 when (not available dpt_mstr) @ dpt_desc
         um
         conv
         qty_inque                        when (not first_op)
         inque_rsn_code                when (not first_op)
         inque_multi_entry                when (not first_op)
         ""                         when (first_op) @ qty_inque
         ""                         when (first_op) @ inque_rsn_code
         ""                         when (first_op) @ inque_multi_entry
         qty_outque
         outque_rsn_code
         outque_multi_entry
         qty_rejque
         rejque_rsn_code
         rejque_multi_entry
      with frame scrap1.
      .

      if is_wiplottrace_enabled()
      then do:
         run init_scrap_input_wip_lot_temptable
            in h_wiplottrace_procs.

         run init_scrap_output_wip_lot_temptable
            in h_wiplottrace_procs.

         run init_scrap_reject_wip_lot_temptable
            in h_wiplottrace_procs.
      end.

      do with frame scrap1 on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.

         for each sr_wkfl where sr_userid = mfguser exclusive-lock:
            delete sr_wkfl.
         end.

         for each pk_det where pk_user = mfguser exclusive-lock:
            delete pk_det.
         end.

         {gprun.i ""rescrapi.p""
            "(input cumwo_lot, input op, output undo_stat)"}
/*GUI*/ if global-beam-me-up then undo, leave.


         if undo_stat then undo main, retry main.

         conv_qty_inque_scrap = qty_inque * conv.
         conv_qty_outque_scrap = qty_outque * conv.
         conv_qty_rejque_scrap = qty_rejque * conv.

         /*CHECK QUEUES IF WOULD GO NEGATIVE; IF SO ISSUE MESSAGES*/

         msg_ct = 0.

         if conv_qty_inque_scrap <> 0 then do:
            /*CHECK INPUT QUEUE*/
            {gprun.i ""rechkq.p"" "(input cumwo_lot, input op,
                 input ""i"",
                 input (- conv_qty_inque_scrap),
                 input-output msg_ct)"}
/*GUI*/ if global-beam-me-up then undo, leave.

         end.

         if conv_qty_outque_scrap <> 0 then do:
            /*CHECK OUTPUT QUEUE*/
            {gprun.i ""rechkq.p"" "(input cumwo_lot, input op,
                 input ""o"",
                 input (- conv_qty_outque_scrap),
                 input-output msg_ct)"}
/*GUI*/ if global-beam-me-up then undo, leave.

         end.

         if conv_qty_rejque_scrap <> 0 then do:
            /*CHECK REJECT QUEUE*/
            {gprun.i ""rechkq.p"" "(input cumwo_lot, input op,
                 input ""r"",
                 input (- conv_qty_rejque_scrap),
                 input-output msg_ct)"}
/*GUI*/ if global-beam-me-up then undo, leave.

         end.

         /*FORCE A PAUSE IF NECESSARY*/
         {gprun.i ""repause.p"" "(input msg_ct)"}
/*GUI*/ if global-beam-me-up then undo, leave.


         if is_wiplottrace_enabled()
            and is_operation_queue_lot_controlled(cumwo_lot, op,
            OUTPUT_QUEUE)
         then do:
            if qty_inque <> 0
               and is_operation_queue_lot_controlled(cumwo_lot, op,
               INPUT_QUEUE)
            then do:
               run get_scrap_input_wip_lots_from_user
                  in h_wiplottrace_procs
                  (input cumwo_lot, input op, input qty_inque, input conv,
                  input um, input site, input wkctr, input mch,
                  output undo_stat).

               if undo_stat then do:
                  undo, retry.
               end.
            end.

            if qty_outque <> 0 then do:
               run get_scrap_output_wip_lots_from_user
                  in h_wiplottrace_procs
                  (input cumwo_lot, input op, input qty_outque,
                  input conv, input um,
                  input site, input wkctr, input mch,
                  output undo_stat).

               if undo_stat then do:
                  undo, retry.
               end.
            end.

            if qty_rejque <> 0 then do:
               run get_scrap_reject_wip_lots_from_user
                  in h_wiplottrace_procs
                  (input cumwo_lot, input op, input qty_rejque,
                  input conv, input um,
                  input site, input wkctr, input mch,
                  output undo_stat).

               if undo_stat then do:
                  undo, retry.
               end.
            end.
         end.

         view frame a.
         view frame scrap1.

         yn = yes.
         /* PLEASE CONFIRM UPDATE */
         {pxmsg.i &MSGNUM=32 &ERRORLEVEL=1 &CONFIRM=yn}

         if yn <> yes then undo, retry.
      end.
/*GUI*/ if global-beam-me-up then undo, leave.


      /****************************************************************/
      /*GOT ALL DATA AND VALIDATED IT, NOW WE CAN DO SOMETHING WITH IT*/
      /****************************************************************/

      /*NO TRANSACTION SHOULD BE PENDING HERE*/
      {gprun.i ""gpistran.p"" "(input 1, input """")"}
/*GUI*/ if global-beam-me-up then undo, leave.


      if is_wiplottrace_enabled()
         and is_operation_queue_lot_controlled(cumwo_lot, op,
         OUTPUT_QUEUE)
      then do:
         run scrap_scrap_wip_lots in h_wiplottrace_procs
            (cumwo_lot, op, emp, site, wkctr, mch, dept, shift, eff_date,
            conv).
      end.
      else do:

         if not first_op then do:
            /*FOR THE INPUT QUEUE, WE MOVE THE SCRAP QTY BACK TO
             * THE PRIOR OP OUTPUT QUEUE AND SCRAP IT THERE.  THIS IS
             * SO USAGE VARIANCES CAN BE CALCULATED CORRECTLY.*/

            if inque_multi_entry then do:
               do i = 1 to 10:
                  if inque_scrap_quantities[i] <> 0 then do:
                     conv_qty_inque_scrap = inque_scrap_quantities[i] *
                     conv.

                     trans_type = "SCRAP-I".

                     {gprun.i ""reophist.p"" "(input trans_type,
                          input cumwo_lot, input prev_op, input emp,
                          input wkctr, input mch, input dept, input shift,
                          input eff_date,
                          output ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.


                     {gprun.i ""removea.p"" "(input cumwo_lot,
                          input prev_op,
                          input eff_date, input ophist_recid,
                          input (- conv_qty_inque_scrap))"}
/*GUI*/ if global-beam-me-up then undo, leave.


                     {gprun.i ""rescrapa.p""
                        "(input cumwo_lot, input prev_op,
                          input conv_qty_inque_scrap,
                          input inque_scrap_rsn_codes[i],
                          input yes, input emp, input ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.

                  end.
               end.
            end.
            else
               if conv_qty_inque_scrap <> 0 then do:
               trans_type = "SCRAP-I".

               {gprun.i ""reophist.p"" "(input trans_type,
                    input cumwo_lot, input prev_op, input emp,
                    input wkctr, input mch, input dept, input shift,
                    input eff_date,
                    output ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.


               {gprun.i ""removea.p"" "(input cumwo_lot, input prev_op,
                    input eff_date, input ophist_recid,
                    input (- conv_qty_inque_scrap))"}
/*GUI*/ if global-beam-me-up then undo, leave.


               {gprun.i ""rescrapa.p""
                  "(input cumwo_lot, input prev_op,
                    input conv_qty_inque_scrap,
                    input inque_rsn_code,
                    input yes, input emp, input ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.

            end.
         end.

         if outque_multi_entry then do:
            do i = 1 to 10:
               if outque_scrap_quantities[i] <> 0 then do:
                  conv_qty_outque_scrap = outque_scrap_quantities[i] *
                  conv.

                  trans_type = "SCRAP-O".

                  {gprun.i ""reophist.p"" "(input trans_type,
                       input cumwo_lot, input op, input emp,
                       input wkctr, input mch, input dept, input shift,
                       input eff_date,
                       output ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.


                  {gprun.i ""rescrapa.p""
                     "(input cumwo_lot, input op,
                       input conv_qty_outque_scrap,
                       input outque_scrap_rsn_codes[i],
                       input yes, input emp, input ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.

               end.
            end.
         end.
         else
            if conv_qty_outque_scrap <> 0 then do:
            trans_type = "SCRAP-O".

            {gprun.i ""reophist.p""
               "(input trans_type, input cumwo_lot, input op, input emp,
                 input wkctr, input mch, input dept, input shift,
                 input eff_date,
                 output ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.


            {gprun.i ""rescrapa.p""
               "(input cumwo_lot, input op,
                 input conv_qty_outque_scrap, input outque_rsn_code,
                 input yes, input emp, input ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.

         end.

         if rejque_multi_entry then do:
            do i = 1 to 10:
               if rejque_scrap_quantities[i] <> 0 then do:
                  conv_qty_rejque_scrap = rejque_scrap_quantities[i] *
                  conv.

                  trans_type = "SCRAP-R".

                  {gprun.i ""reophist.p"" "(input trans_type,
                       input cumwo_lot, input op, input emp,
                       input wkctr, input mch, input dept, input shift,
                       input eff_date,
                       output ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.


                  {gprun.i ""rescrapa.p""
                     "(input cumwo_lot, input op,
                       input conv_qty_rejque_scrap,
                       input rejque_scrap_rsn_codes[i],
                       input no, input emp, input ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.

               end.
            end.
         end.
         else
            if conv_qty_rejque_scrap <> 0 then do:
            trans_type = "SCRAP-R".

            {gprun.i ""reophist.p"" "(input trans_type,
                 input cumwo_lot, input op, input emp,
                 input wkctr, input mch, input dept, input shift,
                 input eff_date,
                 output ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.


            {gprun.i ""rescrapa.p""
               "(input cumwo_lot, input op,
                 input conv_qty_rejque_scrap, input rejque_rsn_code,
                 input no, input emp, input ophist_recid)"}
/*GUI*/ if global-beam-me-up then undo, leave.

         end.

         global_addr = string(ophist_recid).
         {gprun.i ""reintchk.p"" "(input cumwo_lot)"}
/*GUI*/ if global-beam-me-up then undo, leave.

      end.
   end.
/*GUI*/ if global-beam-me-up then undo, leave.

end.
/*GUI*/ if global-beam-me-up then undo, leave.

if is_wiplottrace_enabled() then
   delete PROCEDURE h_wiplottrace_procs no-error.
if is_wiplottrace_enabled() then
   delete PROCEDURE h_wiplottrace_funcs no-error.
